﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ServiceTester {
    using ServiceTester.ServerRentIt11;

    public partial class frmManageAccount : Form {
        public UserSession uSession;
        public RentItClient rClient;

        public frmManageAccount(frmUserClient fromClient) {
            if (fromClient != null) {
                InitializeComponent();
                uSession = fromClient.uSession;
                rClient = fromClient.rClient;
            }else {
                throw new ArgumentException("A User Client Form must be specified!");
            }
        }

        private void btnCreateAccountCreate_Click(object sender, EventArgs e) {
            btnCreateAccountCreate.Enabled = false;

            var bday = DateTime.Now;
            DateTime.TryParse(txtCreateAccountBirthday.Text, out bday);

            var res = rClient.CreateAccount(
                txtCreateAccountName.Text,
                txtCreateAccountPassword.Text,
                txtCreateAccountEmail.Text,
                bday);

            lblCreateAccountReturnCode.Text = res.ToString();

            btnCreateAccountCreate.Enabled = true;
        }

        private void btnEditAccountEdit_Click(object sender, EventArgs e) {
            btnEditAccountEdit.Enabled = false;

            if (uSession != null && uSession.UserId > 0) {
                DateTime? newBday = null;
                var bday = DateTime.Now;
                if (DateTime.TryParse(txtEditAccountBirthday.Text, out bday)) {
                    newBday = bday;
                }

                var res = rClient.UpdateAccount(
                    uSession, txtEditAccountPassword.Text, txtEditAccountEmail.Text, newBday);

                lblEditAccountReturnCode.Text = res.ToString();
            } else {
                MessageBox.Show(
                    "You have to be logged in to edit the account!",
                    "Login Required",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }

            btnEditAccountEdit.Enabled = true;
        }

        private void btnDeleteAccountDelete_Click(object sender, EventArgs e) {
            btnDeleteAccountDelete.Enabled = false;

            if (uSession != null && uSession.UserId > 0) {
                var res = rClient.DeleteAccount(
                    uSession, txtDeleteAccountPassword.Text);

                lblDeleteAccountReturnCode.Text = res.ToString();
            } else {
                MessageBox.Show(
                    "You have to be logged in to delete the account!",
                    "Login Required",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
            }

            btnDeleteAccountDelete.Enabled = true;
        }

        private void btnGetActivationCodeGet_Click(object sender, EventArgs e) {
            btnGetActivationCodeGet.Enabled = false;

            var userId = -1;
            int.TryParse(txtGetActivationCodeUserId.Text, out userId);

            var res = rClient.GetActivationCode(userId);
            txtGetActivationCodeCode.Text = res ?? "Activation not required";

            btnGetActivationCodeGet.Enabled = true;
        }

        private void btnActivateAccountActivate_Click(object sender, EventArgs e) {
            btnActivateAccountActivate.Enabled = false;

            var userId = -1;
            int.TryParse(txtActivateAccountUserId.Text, out userId);

            var res = rClient.ActivateAccount(userId, txtActivateAccountCode.Text);
            lblActivateAccountResult.Text = res ? "Activated" : "Failure";

            btnActivateAccountActivate.Enabled = true;
        }
    }
}
